/**
 * Created by yunpeng on 2014/12/18.
 */
var debug = require('debug')('lifegoods');
var Table = require('../utils/commonDBConn').Table;
var DataGrid = require('../models/DataGrid');

var LifeGoods = function(gname,price,describe,lid,syid,img,createtime){
    this.gid=null;
    this.gname = gname;
    this.price = price;
    this.describe = describe;
    this.lid = lid;
    this.syid = syid;
    this.img = img;
    this.createtime = createtime;

    if(typeof this.save !='function'){
        LifeGoods.prototype.save = function(callback){
            new Table('life_goods').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.update !='function'){
        LifeGoods.prototype.update = function(callback){
            new Table('life_goods').update('gid', this, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.delBusGoods !='function'){
        LifeGoods.prototype.delBusGoods = function(ids, callback){
            new Table('life_goods').delete('gid', ids, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.updateStatus !='function'){
        LifeGoods.prototype.updateStatus = function(id, callback){
            var val = {isused:1}
            new Table('user_lifegoods').updateColumn('id', id,val, function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getList !='function'){//商家列表
        LifeGoods.prototype.getList = function(params,page,callback){
            var columns = '  l.gid,l.gname,l.price,l.describe,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime,ls.lname,l.syid,l.lid,l.img,ls.stypeid,ls.cid,s.industry  ';
            var condition1 =' AND l.lid=ls.lid AND l.syid=s.id ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.gname LIKE '%"+params[2]+"%'";
            }
            if(params[3]){//商家名称模糊查询
                condition1+=" AND ls.lid = '"+params[3]+"'";
            }
            new Table(' life_goods l,life_service ls,service_industry s ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }

    if(typeof this.getListOfUser !='function'){//商家列表
        LifeGoods.prototype.getListOfUser = function(params,page,userid,callback){
            var columns = ' l.id,l.isused,date_format(l.createtime,"%Y-%m-%d %H:%i:%s") as createtime, ul.gid,ul.gname,ul.price,ul.describe,ul.syid,ul.lid,ul.img,s.industry ';
            var condition1 =' AND ul.gid in (SELECT lg.gid FROM user_lifegoods ul,life_goods lg WHERE ul.gid=lg.gid AND ul.userid='+userid+' GROUP BY lg.gid) and ul.syid=s.id AND ul.gid=l.gid ';
            if(params[0]){//开始时间
                condition1+=" AND l.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                condition1+=" AND l.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                condition1+=" AND l.gname LIKE '%"+params[2]+"%'";
            }
            if(params[3]){//商家名称模糊查询
                condition1+=" AND ul.lid = '"+params[3]+"'";
            }
            new Table(' user_lifegoods l,service_industry s,life_goods ul ').queryPage(columns,condition1,page,function(err,result){
                //console.log(result);
                callback(result);
            });
        }
    }


    if(typeof this.getOrderListOfLid !='function'){//商家订单列表
        /**
         * SELECT ods.*,us.id,us.phone,us.integral,us.img as pic FROM (SELECT lg.gid, lg.gname,lg.img,lg.price,ul.createtime,ul.isused,ul.userid,s.industry
         from life_goods lg,users u,user_lifegoods ul,service_industry s
         WHERE lg.lid = u.lid AND u.id=36 AND ul.gid=lg.gid AND lg.syid=s.id) ods,users us WHERE ods.userid=us.id AND us.phone ='13717570104'
         */

        LifeGoods.prototype.getOrderListOfLid = function(params,page,userid,lid,callback){

            var sql = ' FROM (SELECT lg.gid, lg.gname,lg.img,lg.price,date_format(ul.createtime,"%Y-%m-%d %H:%i:%s") as createtime,' +
                    'ul.id as ulid,ul.out_trade_no,ul.trade_no,ul.trade_status,ul.buyer_email,ul.total_fee,ul.gmt_create,ul.gmt_payment,'+
                'ul.isused,ul.userid,s.industry from life_goods lg,users u,user_lifegoods ul,service_industry s WHERE lg.lid = u.lid AND ';
            if(userid){
                sql+=' u.id='+userid;
            }else{
                sql+=' lg.lid='+lid
            }

            sql+='  AND ul.gid=lg.gid AND lg.syid=s.id) ods,users us WHERE ods.userid=us.id ';

            if(params[0]){//开始时间
                sql+=" AND ods.createtime>'"+params[0]+"'";
            }
            if(params[1]){//结束时间
                sql+=" AND ods.createtime<'"+params[1]+"'";
            }
            if(params[2]){//商家名称模糊查询
                sql+=" AND us.phone LIKE '%"+params[2]+"%'";
            }

            new Table().executeSql('SELECT count(*) as count '+sql, [], function(err, res){
                if(err){
                    debug(err);
                    callback({});
                }else{
                    sql+=' ORDER BY ods.'+page[0]+' '+page[1]+' LIMIT '+page[2]+','+page[3];
                    new Table().executeSql('SELECT  ods.*,us.id,us.phone,us.integral,us.img as pic '+sql, [], function(err, result){
                        if(err){
                            debug(err);
                            callback({});
                        }else{
                            callback(new DataGrid(res[0].count,result));
                        }
                    });
                }
            });

        }
    }

    if(typeof this.getBusListByCity !='function'){//所选城市内商家列表
        LifeGoods.prototype.getBusListByCity = function(cid, callback){
            new Table().executeSql("SELECT lid, lname, stypeid FROM life_service WHERE cid=?", [cid], function(err, result){
                if(err){
                    debug(err);
                    callback(['']);
                }else{
                    callback(result);
                }
            })
        }
    }
}

var Industry = function(industry,sid,describe){
    this.id = null;
    this.industry = industry;
    this.sid = sid;
    this.describe = describe;

    if(typeof this.saveIndustry !='function'){
        Industry.prototype.saveIndustry = function(callback){
            new Table('service_industry').insert(this,function(err,result){
                if(err){
                    callback(false);
                }else{
                    callback(true);
                }
            });
        }
    }

    if(typeof this.getSyList !='function'){//所选城市内商家列表
        Industry.prototype.getSyList = function(sid, callback){
            new Table().executeSql("SELECT id, industry, sid FROM service_industry WHERE sid=?", [sid], function(err, result){
                if(err){
                    debug(err);
                    callback(['']);
                }else{
                    callback(result);
                }
            });
        }
    }
}

var UserLifeGoods = function(userid,gid,isused,date) {
    this.id = null;
    this.userid = userid;
    this.gid = gid;
    this.isused = isused;
    this.createtime = date;

    if (typeof this.save != 'function') {
        UserLifeGoods.prototype.save = function (callback) {
            new Table('user_lifegoods').insert(this, function (err, result) {
                if (err) {
                    callback(false);
                } else {
                    callback(true);
                }
            });
        }
    }
}

var Users = function(phone,password,createtime,modelnum,carnum,insuranceDate,usertype) {
    this.id = null;
    this.phone = phone;
    this.password = password;//md5加密
    this.createtime = createtime;
    this.modelnum = modelnum;
    this.carnum = carnum;
    this.insuranceDate = insuranceDate;
    this.usertype = usertype;

    if (typeof this.save != 'function') {
        Users.prototype.save = function (callback) {
            new Table('users').insert(this, function (err, result) {
                if (err) {
                    callback(false);
                } else {
                    callback(true);
                }
            });
        }
    }

    if (typeof this.checkUser != 'function') {
        Users.prototype.checkUser = function (phone,callback) {
            new Table().executeSql("SELECT * FROM users WHERE phone=?", [phone], function(err, result){
                if(err){
                    debug(err);
                    callback(false);
                }else{
                    if(result.length>0){
                        callback('hasUser');
                    }else{
                        callback(true);
                    }

                }
            });
        }
    }


}

module.exports.LifeGoods = LifeGoods;
module.exports.Industry  = Industry;
module.exports.UserLifeGoods  = UserLifeGoods;
module.exports.Users  = Users;
